package controle;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import controleBD.BDInsert;

 

public class ArduinoConnect {
	
	static final String PORTNAME_STRING = "COM16";
	
	private static InputStream in;
    private static OutputStream out;
	
	public ArduinoConnect() throws Exception
    {
        super();
        connect(PORTNAME_STRING);
        
    }
    
    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                serialPort.addEventListener(new SerialReader());
                serialPort.notifyOnDataAvailable(true);
                
                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();
                  
            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    

	public static InputStream getIn() {
		return in;
	}

	public static OutputStream getOut() {
		return out;
	}
	
	
    public static class SerialReader implements SerialPortEventListener 
    {
        private byte[] buffer = new byte[1024];
        private byte[] buffer2 = new byte[1024];
        
        public void serialEvent(SerialPortEvent arg0) {
            int data;
            
            try
            {
            	int len = 0;
                while ( ( data = in.read()) > -1 )
                {
                    if ( data == '\n' ) {
                        break;
                    }
                    
                    buffer[len++] = (byte) data;
                }
                
                int len2 = 0;
                while ( ( data = in.read()) > -1 )
                {
                    if ( data == '\n' ) {
                        break;
                    }
                    
                    buffer2[len2++] = (byte) data;
                }
                
                BDInsert.adiciona(new String(buffer,0,len),new String(buffer2,0,len2-1));
         
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                System.exit(-1);
            }             
        }

    }

}